package leetcode_acm.leetcode_1;

import java.util.Scanner;
import java.util.Stack;

/**
 * 功能描述
 *
 * @author: 张庭杰
 * @date: 2023年03月07日 20:10
 */
public class IsValid {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String ans = scanner.nextLine();
        System.out.println(isValid(ans));
    }

    public static boolean isValid(String s) {
        char[] str = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        for(int i = 0;i<str.length;i++){
            if(str[i] == '(' || str[i] == '{' || str[i] == '['){
                stack.push(str[i]);
            }else{
                if(stack.empty()){
                    return false;
                }else{
                    Character pop = stack.pop();
                    if((pop.equals('(') && str[i] == ')') || (pop.equals('[') && str[i] == ']') || (pop.equals('{') && str[i] == '}')){
                        continue;
                    }else{
                        return false;
                    }
                }
            }
        }
        return stack.isEmpty();
    }
}
